<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Bigger Picture</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="up" href="../performance.html" title="Performance">
<link rel="prev" href="boost_convert_overhead.html" title="Boost.Convert Overhead">
<link rel="next" href="../other_conversions.html" title="Beyond Basic Conversions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="spirit-nav">
<a accesskey="p" href="boost_convert_overhead.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../performance.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../other_conversions.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_convert.performance.the_bigger_picture"></a><a class="link" href="the_bigger_picture.html" title="The Bigger Picture">The Bigger
      Picture</a>
</h3></div></div></div>
<p>
        The presented conversion-related performance data (and potential performance-related
        concerns) have to be taken in perspective. A special project with serious
        high performance requirements is most likely to require special solutions
        which are most likely to cast out many generic libraries. Still, the existence
        of such projects did not kill or diminish in any way the importance of generic
        libraries. And <span class="emphasis"><em>Boost.Convert</em></span> is one such <span class="emphasis"><em>generic</em></span>
        library.
      </p>
<p>
        More so, if performance is a concern, then, in all likelihood, for the great
        majority of the mainstream projects <span class="emphasis"><em>Boost.Convert</em></span> will
        be far from the top on the performance profiler's list. For example, to measure
        conversion-related overhead relative to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>-related
        overhead, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> was replaced with a custom <span class="emphasis"><em>small-string</em></span>
        type:
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_string</span>
<span class="special">{</span>
    <span class="keyword">using</span>      <span class="identifier">this_type</span> <span class="special">=</span> <span class="identifier">my_string</span><span class="special">;</span>
    <span class="keyword">using</span>     <span class="identifier">value_type</span> <span class="special">=</span> <span class="keyword">char</span><span class="special">;</span>
    <span class="keyword">using</span>       <span class="identifier">iterator</span> <span class="special">=</span> <span class="identifier">value_type</span><span class="special">*;</span>
    <span class="keyword">using</span> <span class="identifier">const_iterator</span> <span class="special">=</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">*;</span>

    <span class="identifier">my_string</span> <span class="special">();</span>
    <span class="identifier">my_string</span> <span class="special">(</span><span class="identifier">const_iterator</span><span class="special">,</span> <span class="identifier">const_iterator</span> <span class="special">=</span><span class="number">0</span><span class="special">);</span>

    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">c_str</span> <span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">storage_</span><span class="special">;</span> <span class="special">}</span>
    <span class="identifier">const_iterator</span> <span class="identifier">begin</span> <span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">storage_</span><span class="special">;</span> <span class="special">}</span>
    <span class="identifier">const_iterator</span>   <span class="identifier">end</span> <span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">storage_</span> <span class="special">+</span> <span class="identifier">strlen</span><span class="special">(</span><span class="identifier">storage_</span><span class="special">);</span> <span class="special">}</span>
    <span class="identifier">this_type</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=</span> <span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*);</span>

    <span class="keyword">private</span><span class="special">:</span>

    <span class="keyword">static</span> <span class="identifier">size_t</span> <span class="keyword">const</span> <span class="identifier">size_</span> <span class="special">=</span> <span class="number">12</span><span class="special">;</span>
    <span class="keyword">char</span> <span class="identifier">storage_</span><span class="special">[</span><span class="identifier">size_</span><span class="special">];</span>
<span class="special">};</span>
</pre>
<p>
      </p>
<p>
        The class above is essentially a glorified 12-bytes character buffer sufficient
        to accommodate string representations of 32-bit integers. Deployed with the
        same converters and the same algorithms:
      </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">printf</span><span class="special">(</span><span class="string">"strtol int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">strtol</span><span class="special">()),</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">strtol</span><span class="special">()));</span>
<span class="identifier">printf</span><span class="special">(</span><span class="string">"spirit int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">()),</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">()));</span>
<span class="identifier">printf</span><span class="special">(</span><span class="string">"stream int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span><span class="special">()),</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span><span class="special">()));</span>
<span class="identifier">printf</span><span class="special">(</span><span class="string">"charconv int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">charconv</span><span class="special">()),</span>
       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">charconv</span><span class="special">()));</span>
</pre>
<p>
      </p>
<p>
        It produced the following relative results (compiled with <code class="computeroutput"><span class="identifier">gcc</span>
        <span class="special">-</span><span class="identifier">O3</span></code>):
      </p>
<pre class="programlisting"><span class="identifier">gcc</span><span class="special">-</span><span class="number">4.6</span><span class="special">.</span><span class="number">3</span> <span class="special">(</span><span class="identifier">no</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span> <span class="identifier">optimization</span><span class="special">)</span>
<span class="identifier">strtol</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">1.41</span><span class="special">/</span><span class="number">0.53</span> <span class="identifier">seconds</span><span class="special">.</span>
<span class="identifier">spirit</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">1.98</span><span class="special">/</span><span class="number">1.04</span> <span class="identifier">seconds</span><span class="special">.</span>
<span class="identifier">stream</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">2.49</span><span class="special">/</span><span class="number">1.40</span> <span class="identifier">seconds</span><span class="special">.</span>

<span class="identifier">gcc</span><span class="special">-</span><span class="number">11.2</span><span class="special">.</span><span class="number">0</span> <span class="special">(</span><span class="identifier">with</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span> <span class="identifier">optimization</span><span class="special">)</span>
<span class="identifier">strtol</span>   <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">0.29</span><span class="special">/</span><span class="number">0.27</span> <span class="identifier">seconds</span><span class="special">.</span>
<span class="identifier">spirit</span>   <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">0.37</span><span class="special">/</span><span class="number">0.37</span> <span class="identifier">seconds</span><span class="special">.</span>
<span class="identifier">stream</span>   <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">0.67</span><span class="special">/</span><span class="number">0.61</span> <span class="identifier">seconds</span><span class="special">.</span>
<span class="identifier">charconv</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">0.28</span><span class="special">/</span><span class="number">0.28</span> <span class="identifier">seconds</span><span class="special">.</span>
</pre>
<p>
        The results suggest that for the projects that deal with string-related conversions
        and routinely deploy <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
        facilities the conversion-related overhead will be outweighed by the overhead
        of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> memory management related operations.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2022 Vladimir
      Batov<p>
        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_convert_overhead.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../performance.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../other_conversions.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
